<?php 
/*退款单管理*/
	define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
require_once(ROOT_PATH . 'includes/lib_goods.php');
@ini_set('display_errors',        2);


// 退款单列表
if ($_REQUEST['act'] == 'refund_list')
{   
    /* 检查权限 */
    admin_priv('back_view');
    $smarty->assign('refund_stat', $_LANG['rt']);   // shou
    $smarty->assign('ur_here',"退款单列表");
    // $smarty->assign('action_link', array('href' => 'back.php?act=add', 'text' => '添加退货单'));
    $refund_list=refund_list();//获取退货单列表





    $smarty->assign('full_page',       1);
    $smarty->assign('filter',       $refund_list['filter']);
    $smarty->assign('record_count', $refund_list['record_count']);
    $smarty->assign('page_count',   $refund_list['page_count']);
    $smarty->assign('refund_list',$refund_list['refund_list']);
    $smarty->display('refund/refund_list.htm');
}
//分页
elseif ($_REQUEST['act'] == 'refund_query')
{
    $refund_list = refund_list();//订单列表
    $smarty->assign('filter',       $refund_list['filter']);
    $smarty->assign('record_count', $refund_list['record_count']);
    $smarty->assign('page_count',   $refund_list['page_count']);
    $smarty->assign('refund_list',$refund_list['refund_list']);

    make_json_result($smarty->fetch('refund/refund_list.htm'), '', array('filter' => $refund_list['filter'], 'page_count' => $refund_list['page_count']));

}

//确认退款单金额
elseif ($_REQUEST['act'] == 'confirm_refund')
{	
	$refund_id = !empty($_REQUEST['refund_id'])?$_REQUEST['refund_id']:'';//退款单号
	$sql = "SELECT * FROM ".$ecs->table('refund') ." WHERE refund_id=$refund_id ";
	$refund_info=$db->getRow($sql);
	//更改状态为已支付
	$sql="UPDATE ".$ecs->table('refund') ." SET refund_stat = 2 ,confirm_time = '".time()."'  WHERE refund_id=$refund_id ";
	$db->query($sql);
	//记录客服操作
        $content =  "【标题】客服已退款 <br/>".
                    "【内容】<br/>".
                    " 客服已将".$refund_info['refund_money']."元退到你填写的支付宝中<br/>";
        $handle_record = array(
        'handle_user'       => $_SESSION['admin_name'],
        'service_id'        => $refund_info['service_id'],
        'add_time'          => time(),
        'content'           => $content,
        'handle_id'         => 2,
        );
        $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('handle_record'), $handle_record, 'INSERT', '', 'SILENT'); 
        //更新售后状态
        $sql = " UPDATE ".$GLOBALS['ecs']->table('sale_service') .
               " SET service_state = 3 ,handle_state = 7 ".
               " WHERE service_id = $refund_info[service_id]";
        $db->query($sql);
        $links[] = array('href' => 'refund.php?act=refund_list', 'text' => "操作成功");
        sys_msg("操作成功", 100, $links);
}
elseif ($_REQUEST['act'] == 'changge_refund')
{
    $refund_id    = !empty($_REQUEST['refund_id'])?$_REQUEST['refund_id']:'';//退款单号
    $content      = !empty($_REQUEST['content'])?$_REQUEST['content']:'';//操作备注
    $refund_money = !empty($_REQUEST['refund_money'])?$_REQUEST['refund_money']:'0';//退款金额
    /*查询退款单信息*/
    $sql = "SELECT * FROM ".$ecs->table('refund') ." WHERE refund_id=$refund_id ";
    $refund_info=$db->getRow($sql);
    /*修改退款单金额*/
    $sql = " UPDATE ".$GLOBALS['ecs']->table('refund') .
           " SET refund_money = '$refund_money' ,refund_stat = 2 ,confirm_time = '".time()."' WHERE refund_id=$refund_id ";
    $db->query($sql);
    //记录客服操作
    $handle_record = array(
    'handle_user'       => $_SESSION['admin_name'],
    'service_id'        => $refund_info['service_id'],
    'add_time'          => time(),
    'content'           => $content,
    'handle_id'         => 2,
    );
    $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('handle_record'), $handle_record, 'INSERT', '', 'SILENT'); 
    //更新售后状态
    $sql = " UPDATE ".$GLOBALS['ecs']->table('sale_service') .
           " SET service_state = 3 ,handle_state = 7 ".
           " WHERE service_id = $refund_info[service_id]";
    $db->query($sql);
    $links[] = array('href' => 'refund.php?act=refund_list', 'text' => "操作成功");
    sys_msg("操作成功", 100, $links);
 }
//退款单列表
function refund_list(){

        $where = 'WHERE 1 ';
       

        /* 分页大小 */
        $filter['page'] = empty($_REQUEST['page']) || (intval($_REQUEST['page']) <= 0) ? 1 : intval($_REQUEST['page']);
        if (isset($_REQUEST['page_size']) && intval($_REQUEST['page_size']) > 0)
        {
            $filter['page_size'] = intval($_REQUEST['page_size']);
        }
        elseif (isset($_COOKIE['ECSCP']['page_size']) && intval($_COOKIE['ECSCP']['page_size']) > 0)
        {
            $filter['page_size'] = intval($_COOKIE['ECSCP']['page_size']);
        }
        else
        {
            $filter['page_size'] = 15;
        }

        $sql= "SELECT * FROM ".$GLOBALS['ecs']->table('refund') .$where."  ORDER BY add_time DESC";
        $filter['record_count'] = $GLOBALS['db']->num_rows($GLOBALS['db']->query($sql)); // 总数
        $filter['page_count']     = $filter['record_count'] > 0 ? ceil($filter['record_count'] / $filter['page_size']) : 1;//总数
        $sql = $sql." LIMIT " . ($filter['page'] - 1) * $filter['page_size'] . ",$filter[page_size]";
    
        $row = $GLOBALS['db']->getAll($sql);
            
        foreach ($row as $key => $value) {
            $row[$key]['add_time'] =date('Y-m-d-H:i:s',$value['add_time']);
            if(!empty($value['confirm_time'])){
                $row[$key]['confirm_time'] =date('Y-m-d-H:i:s',$value['confirm_time']);
            }
            else{
                 $row[$key]['confirm_time'] ="未退款";
            }
        }
        $arr = array('refund_list' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
        return $arr;
}













 ?>